package com.jialanmo.zhihudaily.ui.theme;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jialanmo.helper.recyclerview.HeaderAndFooterAdapter;
import com.jialanmo.helper.OnItemClickListener;
import com.jialanmo.helper.recyclerview.OnViewCreateListener;
import com.jialanmo.zhihudaily.R;
import com.jialanmo.zhihudaily.data.model.News;
import com.jialanmo.zhihudaily.data.model.Story;
import com.jialanmo.zhihudaily.data.model.Theme;
import com.jialanmo.zhihudaily.ui.main.StoryAdapter;
import com.jialanmo.zhihudaily.ui.news.NewsActicity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public final class ThemeFragment extends Fragment implements ThemeContract.View {

    private ThemeContract.Presenter mPresenter;
    private final HeaderAndFooterAdapter<Object> mStoryAdapter = new HeaderAndFooterAdapter<>(new StoryAdapter(new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Object data = mStoryAdapter.getData().get(position);
            if (data instanceof Story) {
                Intent intent = new Intent(getContext(), NewsActicity.class);
                intent.putExtra(News.NEWS_ID, ((Story) data).getId());
                startActivity(intent);
            }
        }
    }));

    private View mView;
    @BindView(R.id.fragment_main_swiperefreshlayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.fragment_main_recyclerview_latest)
    RecyclerView mRecyclerView;
    ImageView mTopStoryImageView;
    TextView mTopStoryTextView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            int id = getArguments().getInt(Theme.THEME_ID);
            mPresenter = new ThemePresenter(this, id);
            mView = inflater.inflate(R.layout.fragment_main, container, false);
            ButterKnife.bind(this, mView);

            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mStoryAdapter.setHeaderView(new OnViewCreateListener() {
                @Override
                public View onViewCreate(ViewGroup parent) {
                    View mTopStoryView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_main_topsotry_viewpager_item, parent, false);
                    mTopStoryImageView = (ImageView) mTopStoryView.findViewById(R.id.fragment_main_topstory_viewpager_item_imageview_image);
                    mTopStoryTextView = (TextView) mTopStoryView.findViewById(R.id.fragment_main_topstory_viewpager_item_textview_title);
                    return mTopStoryView;
                }
            });
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setAdapter(mStoryAdapter);

            mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) (64 * getContext().getResources().getDisplayMetrics().density));
            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    mPresenter.getTheme();
                }
            });

            mPresenter.start();
        }
        return mView;
    }

    @Override
    public void showTheme(Theme theme) {
        List<Object> objects = new ArrayList<>(theme.getStories().size());
        objects.addAll(theme.getStories());
        mStoryAdapter.getData().clear();
        mStoryAdapter.getData().addAll(objects);
        mStoryAdapter.notifyDataSetChanged();
        Glide.with(getContext()).load(theme.getImage()).into(mTopStoryImageView);
        mTopStoryTextView.setText(theme.getDescription());
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        mSwipeRefreshLayout.setRefreshing(refreshing);
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
